/*
 SAMPLE FORM
*/

require('./common');

var gs3 = require('gs3');
var Database = gs3.Models.Database;
var Entity = gs3.Models.Entity;

var db = new Database(config, true);	

exports.load = function(req, res){		
	res.render("orgchart",{
		title: "Organization Chart",
		nodes : "",
		lines : "",
		startCtr: "1"
	});
}
exports.save = function(req, res){	
	
	var title = req.params.title
	var desc = req.params.desc
	var status = req.params.status
	var nodes = req.params.nodes; // for nodes
	var lines = req.params.lines; // for lines
	var output = title+ "," +desc+ "," +status+ "\n";
	
	var orgchart = new Entity('tborgchart', 'id');

	orgchart.fields.company_id = 0;
	orgchart.fields.title = title;
	orgchart.fields.description = desc;
	orgchart.fields.date = "0";
	orgchart.fields.status = status;
	orgchart.fields.is_active = 1;
	
	db.insert(orgchart, function(err, data) {
		if (err) {
			res.send(err.message);
		} else {
			orgchart_id = data.insertId;
			insertNodes(orgchart_id,nodes);
			res.send(insertLines(orgchart_id,lines));
			if(status==1){
				var company_id = 0;
				deactivateOrgcharts(company_id,orgchart_id);
			}
		}
	});
	
	/*
	
	for(a=0;a<nodes.length;a++){
		if(nodes[a]!=""){
			output+="id = "+nodes[a].split("++")[0]+
			", top = "+nodes[a].split("++")[1]+
			", left = "+nodes[a].split("++")[2]+
			", text = "+nodes[a].split("++")[3]+"\n";	
		}
	}
	
	output+="\n\n";
	
	for(c=0;c<nodes.length;c++){
		if(nodes[c]!=""){
			output+="class = "+lines[c].split("++")[0]+
			", parent = "+lines[c].split("++")[1]+
			", child = "+lines[c].split("++")[2]+
			", top = "+lines[c].split("++")[3]+
			", left = "+lines[c].split("++")[4]+
			", height = "+lines[c].split("++")[4]+
			", width = "+lines[c].split("++")[4]+"\n";	
		}
	}
	
	res.send(orgchart_id);
	*/
}
function insertNodes(orgchart_id,nodes){
	var orgchartObjects = new Entity('tborgchartobjects', 'id');
	nodes = nodes.split("{}");
	for(a=0;a<nodes.length;a++){
		if(nodes[a]!=""){
			orgchartObjects.fields.orgChart_id = orgchart_id;
			orgchartObjects.fields.object_id = nodes[a].split("++")[0];
			orgchartObjects.fields.tops = nodes[a].split("++")[1];
			orgchartObjects.fields.lefts = nodes[a].split("++")[2];
			orgchartObjects.fields.heights = nodes[a].split("++")[3];
			orgchartObjects.fields.widths = nodes[a].split("++")[4];
			orgchartObjects.fields.department = nodes[a].split("++")[5];	
	
			db.insert(orgchartObjects, function(err, data) {
				if (err) {
					res.send(err.message);
				}
			});
		}
	}
}
function insertLines(orgchart_id,lines){
	var orgchartLines = new Entity('tborgchartline', 'id');
	var liness = lines.split("{}");
	var output = "";
	for(a=0;a<liness.length-1;a++){
		if(liness[a]!=""){
			orgchartLines.fields.orgChart_id = orgchart_id;
			orgchartLines.fields.lineClass = liness[a].split("++")[0];
			orgchartLines.fields.parent = liness[a].split("++")[1];
			orgchartLines.fields.child = liness[a].split("++")[2];
			orgchartLines.fields.line_top = liness[a].split("++")[3];
			orgchartLines.fields.line_left = liness[a].split("++")[4];
			orgchartLines.fields.line_height = liness[a].split("++")[5];
			orgchartLines.fields.line_width = liness[a].split("++")[6];
			output+=liness[a].split("++")[0]+liness[a].split("++")[1]+liness[a].split("++")[2]+
			liness[a].split("++")[3]+liness[a].split("++")[4]+liness[a].split("++")[5]+liness[a].split("++")[6];
			db.insert(orgchartLines, function(err, data) {
				if (err) {
					res.send(err.message);
				}else{
					output = data;
				}
			});
		}
	}
	return output;
}

function deactivateOrgcharts(company_id,orgchart_id){
	db.query("Update tborgchart set status = 0 WHERE company_id='"+company_id+"' and id!='"+orgchart_id+"'",function(error, data){
		if(error){
			console.log(error)
		}else{
			console.log(data)
		}
	})
}

/*Count Orgchart*/
exports.getCountOrgchart = function(req, res){
	company_id = 0;
	db.query("SELECT COUNT(*) as count FROM tborgchart where company_id = '"+company_id+"'",function(error, data){
		if(error){
			console.log(error.message)
		}else{
			console.log(data[0].count)
			res.send('' + data[0].count)
		}
		
	})
}
/*Update orgchart Status*/
exports.updateStatus = function(req,res){
	var status = req.body.status;
	var orgchart_id = req.body.id;
	var output = "";
	if(status==0){
		output = "Deactivate: "+orgchart_id+"";
	}else{
		output = "Activate: "+orgchart_id+"";
		//deactivate all orgchart
		//deactivateOrgcharts(company_id,orgchart_id)
	}
	res.send(output);
}


